﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Routing;
using TiTi.tea.Api.Attributes;
using TiTi.tea.Api.Models;
using TiTi.tea.BLL;
using TiTi.tea.Comm;
using TiTi.tea.Model;

namespace TiTi.tea.Api.Controllers
{

    [RoutePrefix("api/UserInfo")]
    public class UserController : ApiController
    {
        private UsreinfoBLL bll=new UsreinfoBLL();
        [Route("")]
        [HttpGet]
        public Result Query(string UserName = "", int PageSize = 10, int PageIndex = 1)
        {
            int total = 0;
            var data = bll.Query(UserName, out total, PageSize, PageIndex);
            return Result.Success(new { data, total });
        }
        [Route("get")]
        [HttpGet]
        public Result get(int id)
        {
            var data = bll.Get(id);
            return Result.Success(data);
        }
        [MyAuthorize]
        [HttpGet]
        [Route("getUserInfo")]
        public Result get()
        {
            IHttpRouteData routeData = Request.GetRouteData();
            IDictionary<string, object> dict = routeData.Values;
            var auth = dict["auth"] as AuthInfo;
            var userId = auth.UserId;

            var data = bll.Get(userId);
            return Result.Success(data);
        }
        [Route("update")]
        [HttpPost]
        public Result update(UserInfo entity)
        {
          UserInfo userinfo = bll.Get(entity.Id);
            userinfo.UserName = entity.UserName;
            userinfo.Sex = entity.Sex;
            userinfo.Tel = entity.Tel;
            if (bll.Update(userinfo))
            {
                return Result.Success(userinfo);
            }
            else
            {
                return Result.Error("修改失败");
            }
            
        }
        [Route("Add")]
        [HttpPost]
        public Result Add(UserInfo entity)
        {

            entity.CreateTime=DateTime.Now;
            entity.IsDelete = 0;
            if (bll.Add(entity))
            {
                return Result.Success(entity);
            }else
            {
                return Result.Error("添加失败");
            }
        }
        [Route("delete")]
        [HttpPost]
        public Result delete(int id)
        {
            return Result.Success(bll.Delete(id));
        }

        [Route("Impower")]
        [HttpPost]

        public Result Impower(ImpowerVM param)
        {
            if (bll.Impower(param.userid, param.roleids))
            {
                return Result.Success();
            }else
            {
                return Result.Error("失败");
            }
        }

        [Route("GetAll")]
        [HttpGet]
        public Result GetAll()
        {
            return Result.Success(bll.Getall());
        }
        [Route("YesImpower")]
        [HttpGet]
        public Result YesImpower(int userid)
        {
            return Result.Success(bll.YesImpower(userid));
        }
    }
}